package net.xo68.destiny.manager.controller;

import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import net.xo68.destiny.contract.domain.StringRespondResult;
import net.xo68.destiny.manager.entity.SystemUser;
import net.xo68.destiny.manager.security.SecurityUser;
import net.xo68.destiny.manager.service.SystemUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;


/**
 * SystemUser controller
 * @author wuxie(16349023@qq.com)
 * @date 2020年11月07日 16:43:18
 *
 */
@Api(value = "用户管理",tags = {"用户管理"})
@RequestMapping("/SystemUser")
@Slf4j
@RestController
public class SystemUserController{


    @Autowired
    private SystemUserService systemUserService;

    @ApiOperation(value="详情")
	@GetMapping("/detail/{id}")
    public SystemUser detail(@PathVariable(name="id") Long id) {
        return systemUserService.find(id);
    }


    @ApiOperation(value="翻页列表")
    @GetMapping("/list")
    public PageInfo<SystemUser> list(@ApiIgnore SecurityUser securityUser, @RequestParam(name="pageIndex",defaultValue="1") int pageIndex, @RequestParam(name="pageSize",defaultValue="10") int pageSize){
        log.info("{}",securityUser);
        return systemUserService.list(pageIndex, pageSize);
    }

    @PreAuthorize("hasRole('ADMIN')")
    @ApiOperation(value="创建")
	@PostMapping
    public SystemUser add(@RequestBody SystemUser entity ) {
        return systemUserService.insert(entity);
    }


    @PreAuthorize("hasRole('ADMIN')")
    @ApiOperation(value="删除")
	@DeleteMapping
	public StringRespondResult delete(@RequestParam(name="id") Long id) {
	    int effect=systemUserService.delete(id);
	    if(effect< 1){
	        return StringRespondResult.fail();
        }
        return StringRespondResult.ok();
	}

    @PreAuthorize("hasRole('ADMIN')")
    @ApiOperation(value="更新")
	@PutMapping
	public SystemUser update(@RequestBody SystemUser entity) {
	    return systemUserService.update(entity);
	}
}